package ru.nnstu.medialib.server;

import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;

import ru.nnstu.medialib.domain.Album;
import ru.nnstu.medialib.domain.Artist;
import ru.nnstu.medialib.domain.Author;
import ru.nnstu.medialib.domain.Book;
import ru.nnstu.medialib.domain.Photo;
import ru.nnstu.medialib.domain.PhotoAlbum;
import ru.nnstu.medialib.domain.Song;
import ru.nnstu.medialib.domain.Video;
import ru.nnstu.medialib.service.AlbumService;
import ru.nnstu.medialib.service.AlbumServiceImpl;
import ru.nnstu.medialib.service.ArtistService;
import ru.nnstu.medialib.service.ArtistServiceImpl;
import ru.nnstu.medialib.service.AuthorService;
import ru.nnstu.medialib.service.AuthorServiceImpl;
import ru.nnstu.medialib.service.BookService;
import ru.nnstu.medialib.service.BookServiceImpl;
import ru.nnstu.medialib.service.PhotoAlbumService;
import ru.nnstu.medialib.service.PhotoAlbumServiceImpl;
import ru.nnstu.medialib.service.PhotoService;
import ru.nnstu.medialib.service.PhotoServiceImpl;
import ru.nnstu.medialib.service.SongService;
import ru.nnstu.medialib.service.SongServiceImpl;
import ru.nnstu.medialib.service.VideoService;
import ru.nnstu.medialib.service.VideoServiceImpl;

public class ServerImpl extends UnicastRemoteObject implements Server {

	ArtistService artistService;
	AlbumService albumService;
	SongService songService;
	AuthorService authorService;
	BookService bookService;
	PhotoAlbumService photoAlbumService;
	PhotoService photoService;
	VideoService videoService;

	protected ServerImpl() throws RemoteException {
		super();
		artistService = new ArtistServiceImpl();
		albumService = new AlbumServiceImpl();
		songService = new SongServiceImpl();
		authorService = new AuthorServiceImpl();
		bookService = new BookServiceImpl();
		photoAlbumService = new PhotoAlbumServiceImpl();
		photoService = new PhotoServiceImpl();
		videoService = new VideoServiceImpl();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("RMI server started");
		// Create and install a security manager
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new RMISecurityManager());
			System.out.println("Security manager installed.");
		} else {
			System.out.println("Security manager already exists.");
		}

		try { // special exception handler for registry creation
			LocateRegistry.createRegistry(1099);
			System.out.println("java RMI registry created.");
		} catch (RemoteException e) {
			// do nothing, error means registry already exists
			System.out.println("java RMI registry already exists.");
		}

		try {
			// Instantiate RmiServer
			ServerImpl obj = new ServerImpl();

			Naming.rebind("//localhost/ServerImpl", obj);

			System.out.println("Server bound in registry");
		} catch (Exception e) {
			System.err.println("RMI server exception:" + e);
			e.printStackTrace();
		}
	}

	@Override
	public List<Artist> listArtist() {
		return artistService.listArtist();
	}

	@Override
	public List<Album> listAlbum() throws RemoteException {
		return albumService.listAlbum();
	}

	@Override
	public List<Song> listSong() throws RemoteException {
		return songService.listSong();
	}

	@Override
	public List<Author> listAuthor() throws RemoteException {
		return authorService.listAuthor();
	}

	@Override
	public List<Book> listBook() throws RemoteException {
		return bookService.listBook();
	}

	@Override
	public List<PhotoAlbum> listPhotoAlbum() throws RemoteException {
		return photoAlbumService.listPhotoAlbum();
	}

	@Override
	public List<Photo> listPhoto() throws RemoteException {
		return photoService.listPhoto();
	}

	@Override
	public List<Video> listVideo() throws RemoteException {
		return videoService.listVideo();
	}

}
